$( document ).ready(function() {
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("[name='searchUserId']").val("");
	$("[name='searchFirstname']").val("");
	$("[name='searchLastname']").val("");
	$("[name='recordPatientId']").val("");
	/*
	$("[name='yearStart']").val("-1");
	$("[name='monthStart']").val("-1");
	$("[name='dayStart']").val("-1");
	$("[name='yearEnd']").val("-1");
	$("[name='monthEnd']").val("-1");
	$("[name='dayEnd']").val("-1");
	*/
	$("#chooseStartDate").datepicker();
	$("#chooseEndDate").datepicker();
	$("#chooseStartDate").val("");
	$("#chooseStartTime").val("");
	$("#chooseEndDate").val("");
	$("#chooseEndTime").val("");
	$("[name='recordDiagnosis']").val("");
	$("[name='recordDrug']").val("");
	$("[name='recordComment']").val("");
});

function getPatientSearchResult() {
	$("#patientResult").empty();
	$("#recordDetail").empty();
	$("#recordDetailView").empty();
	$("#recordDetailView").hide();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$.getJSON("doctor/searchResult.html?searchUserId="+$("[name='searchUserId']").val()+"&searchFirstname="+$("[name='searchFirstname']").val()+"&searchLastname="+$("[name='searchLastname']").val(), function(data) {
		if(!jQuery.isEmptyObject(data.patientSearchResult)){
			$("#patientResult").data("patientSearchResult", data);
			printTableBody(data);
			$("#patientResult").append("<br /><button id=\"confirmPatient\" onclick=\"confirmPatient()\" class=\"button\">confirm</button>");
		}else{
			$("#patientResult").append("<strong>No result.</strong>");
		}
	});
}

function clearPtable(){
	$("#patientResult").empty();
}

function printTableBody(data) {
	
	$("#patientResult")
			.append(
					"<table id=\"list\" class=\"tablecss\">" +
					"<thead>" +
					"<tr>" +
					"<th></th>"+
					"<th>UserId</th>" +
					"<th>First name</th>" +
					"<th>Last name</th>" +
					"<th>Gender</th>" +
					"<th>Date of Birth</th>" +
					"</tr>" +
					"</thead>" +
					"<tbody id=\"tableBody\">" +
					"</tbody>" +
					"</table>");
	var tr;
	$(data).each(
			function(index, value) {
				$(value.patientSearchResult).each(function (i,v){
					tr = $("<tr>");
					tr.append("<td><input type=\"radio\" name=\"patientRadio\" value=\""+v.userId+"\"></td>");
					tr.append("<td>" + v.userId + "</td>");
					tr.append("<td>" + v.firstname + "</td>");
					tr.append("<td>" + v.lastname + "</td>");
					var gen=v.gender;
					if(gen=='m'){
						tr.append("<td>male</td>");
					}else if(gen=='f'){
						tr.append("<td>female</td>");
					}else{
						tr.append("<td></td>");
					}
					tr.append("<td>" + v.dateOfBirth.substring(0,10) +"</td>");
					tr.append("</tr>");
					$("#tableBody").append(tr);
				});
				
			});
	$("#list").tablesorter({
		widgets : [ 'zebra' ],
		headers : {
			0: {
				sorter : false
			}
		}
	});
}

function confirmPatient(){
	var id=$("[name=\"patientRadio\"]:checked").val();
	$("[name='recordPatientId']").val(id);
	$("#patientResult").empty();
	$("#recordDetail").empty();
	$("#recordDetailView").empty();
	$("#recordDetailView").hide();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	/*
	$("[name='yearStart']").val("-1");
	$("[name='monthStart']").val("-1");
	$("[name='dayStart']").val("-1");
	$("[name='yearEnd']").val("-1");
	$("[name='monthEnd']").val("-1");
	$("[name='dayEnd']").val("-1");
	*/
	$("[name='recordDiagnosis']").val("");
	$("[name='recordDrug']").val("");
	$("[name='recordComment']").val("");
	$("#chooseStartDate").val("");
	$("#chooseStartTime").val("");
	$("#chooseEndDate").val("");
	$("#chooseEndTime").val("");
	$.getJSON("doctor/recordList.html?recordListPatientId="+id, function(data){
		if(!jQuery.isEmptyObject(data.recordDetailList)){
			$("#recordDetail").data("recordDetails", data);
			printRecordTableBody(data);
			$("#recordDetail").append("<br /><button id=\"confirmRecord\" onclick=\"confirmRecord()\" class=\"button\">confirm</button>");
		}else{
			$("#recordDetail").append("<strong>No result.</strong>");
		}
	});
}
function printRecordTableBody(data) {
	
	$("#recordDetail")
			.append(
					"<table id=\"list\" class=\"tablecss\">" +
					"<thead>" +
					"<tr>" +
					"<th></th>"+
					"<th>Record id</th>"+
					"<th>Patient id</th>" +
					"<th>Patient name</th>" +
					"<th>Doctor id</th>" +
					"<th>Doctor name</th>" +
					"<th>Start time</th>" +
					"<th>End time</th>" +
					"</tr>" +
					"</thead>" +
					"<tbody id=\"tableBody\">" +
					"</tbody>" +
					"</table>");
	var tr;
	$(data).each(
			function(index, value) {
				$(value.recordDetailList).each(function (i,v){
					tr = $("<tr>");
					tr.append("<td><input type=\"radio\" name=\"recordRadio\" value=\""+v.recordId+"\"></td>");
					tr.append("<td>" + v.recordId + "</td>");
					tr.append("<td>" + v.patientId + "</td>");
					tr.append("<td>" + v.patientName + "</td>");
					tr.append("<td>" + v.doctorId + "</td>");
					tr.append("<td>" + v.doctorName + "</td>");
					tr.append("<td>" + convertDateTime(v.startTime) + "</td>");
					tr.append("<td>" + convertDateTime(v.endTime) + "</td>");
					tr.append("</tr>");
					$("#tableBody").append(tr);
				});
				
			});
	$("#list").tablesorter({
		widgets : [ 'zebra' ],
		headers : {
			0: {
				sorter : false
			}
		}
	});
}

function convertDateTime(milliSec){
	var dateTime=new Date(milliSec);
	var format=new String("");
	format=dateTime.getFullYear()+"-";
	if((dateTime.getMonth()+1)<10){
		format=format+"0"+(dateTime.getMonth()+1)+"-";
	}else{
		format=format+(dateTime.getMonth()+1)+"-";
	}
	if(dateTime.getDate()<10){
		format=format+"0"+dateTime.getDate()+" ";
	}else{
		format=format+dateTime.getDate()+" ";
	}
	if(dateTime.getHours()<10){
		format=format+"0"+dateTime.getHours()+":";
	}else{
		format=format+dateTime.getHours()+":";
	}
	if(dateTime.getMinutes()<10){
		format=format+"0"+dateTime.getMinutes();
	}else{
		format=format+dateTime.getMinutes();
	}
	return format;
}

function confirmRecord(){
	var id=$("[name=\"recordRadio\"]:checked").val();
	$("#patientResult").empty();
	$("#recordDetailView").empty();
	$("#recordDetailView").hide();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	var record=null;
	var recordList=$("#recordDetail").data("recordDetails");
	$(recordList).each(function(index, value){
		$(value.recordDetailList).each(function (i,v){
			if(v.recordId==id){
				record=v;
			}
		});
	});
	if(record!=null){
		$("#recordDetailView").append("<h1>Visit Record</h1><label for=\"rdRecordId\">Record id</label>" +
				"<textarea id=\"rdRecordId\" rows=\"1\" cols=\"10\" readonly>"+record.recordId+"</textarea><br />" +
						"<label for=\"rdPatientId\">Patient id</label>" +
				"<textarea id=\"rdPatientId\" rows=\"1\" cols=\"10\" readonly>"+record.patientId+"</textarea><br />" +
						"<label for=\"rdPatientName\">Patient name</label>" +
				"<textarea id=\"rdPatientName\" rows=\"1\" cols=\"30\" readonly>"+record.patientName+"</textarea><br />" +
						"<label for=\"rdDoctorId\">Doctor id</label>" +
				"<textarea id=\"rdDoctorId\" rows=\"1\" cols=\"10\" readonly>"+record.doctorId+"</textarea><br />" +
						"<label for=\"rdDoctorName\">Doctor name</label>" +
				"<textarea id=\"rdDoctorName\" rows=\"1\" cols=\"30\" readonly>"+record.doctorName+"</textarea><br />"+
						"<label for=\"rdStartTime\">Start time</label>" +
				"<textarea id=\"rdStartTime\" rows=\"1\" cols=\"30\" readonly>"+convertDateTime(record.startTime)+"</textarea><br />"+
						"<label for=\"rdEndTime\">End time</label>" +
				"<textarea id=\"rdEndTime\" rows=\"1\" cols=\"30\" readonly>"+convertDateTime(record.endTime)+"</textarea><br />"+
						"<label for=\"rdDiagnosis\">Diagnosis</label>" +
				"<textarea id=\"rdDiagnosis\" rows=\"5\" cols=\"50\" readonly>"+record.diagnosis+"</textarea><br />" +
						"<label for=\"rdDrug\">Drug</label>" +
				"<textarea id=\"rdDrug\" rows=\"5\" cols=\"50\" readonly>"+record.drug+"</textarea><br />" +
						"<label for=\"rdInstruction\">Instruction</label>" +
				"<textarea id=\"rdInstruction\" rows=\"5\" cols=\"50\" readonly>"+record.instruction+"</textarea><br />" +
						"<label for=\"rdDEAnum\">DEA number</label>" +
				"<textarea id=\"rdDEAnum\" rows=\"5\" cols=\"50\" readonly>"+record.deaNum+"</textarea><br />" +
						"<label for=\"rdTreatSche\">Treatment Schedule</label>" +
				"<textarea id=\"rdTreatSche\" rows=\"5\" cols=\"50\" readonly>"+record.treatmentSchedule+"</textarea><br />" +
						"<label for=\"rdComment\">Comment</label>" +
				"<textarea id=\"rdComment\" rows=\"5\" cols=\"50\" readonly>"+record.comment+"</textarea><br />");
		$("#recordDetailView").show();
	}
}

function getAllPatients(){
	$("#patientResult").empty();
	$("#recordDetail").empty();
	$("#recordDetailView").empty();
	$("#recordDetailView").hide();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("[name='searchUserId']").val("");
	$("[name='searchFirstname']").val("");
	$("[name='searchLastname']").val("");
	$.getJSON("doctor/allPatients.html", function(data) {
		if(!jQuery.isEmptyObject(data.patientSearchResult)){
			$("#patientResult").data("patientSearchResult", data);
			printTableBody(data);
			$("#patientResult").append("<br /><button id=\"confirmPatient\" onclick=\"confirmPatient()\" class=\"button\">confirm</button>");
		}else{
			$("#patientResult").append("<strong>No result.</strong>");
		}
		
	});
}

function getRecordSearchResult(){
	$("#patientResult").empty();
	$("#recordDetail").empty();
	$("#recordDetailView").empty();
	$("#recordDetailView").hide();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$.getJSON("doctor/searchRecord.html?recordPatientId="+$("[name='recordPatientId']").val()+"&startDate="+$("#chooseStartDate").val()+"&endDate="+$("#chooseEndDate").val()+"&recordDiagnosis="+$("[name='recordDiagnosis']").val()+"&recordDrug="+$("[name='recordDrug']").val()+"&recordComment="+$("[name='recordComment']").val(), function(data){
		if(data.flag==false){
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("error");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
		}else if(data.flag==true){
			if(!jQuery.isEmptyObject(data.recordDetailList)){
				$("#recordDetail").data("recordDetails", data);
				printRecordTableBody(data);
				$("#recordDetail").append("<br /><button id=\"confirmRecord\" onclick=\"confirmRecord()\" class=\"button\">confirm</button>");
			}else{
				$("#recordDetail").append("<strong>No result.</strong>");
			}
		}
	});
}